package cn.xuewenbao.edu.service.impl;

import cn.xuewenbao.edu.entity.Book;
import cn.xuewenbao.edu.entity.Course;
import cn.xuewenbao.edu.entity.GoodLink;
import cn.xuewenbao.edu.entity.vo.GoodVo;
import cn.xuewenbao.edu.mapper.GoodLinkMapper;
import cn.xuewenbao.edu.service.BookService;
import cn.xuewenbao.edu.service.CourseService;
import cn.xuewenbao.edu.service.GoodLinkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 好物链接表 服务实现类
 * </p>
 *
 * @author mafayu
 * @since 2021-06-04
 */
@Service
public class GoodLinkServiceImpl extends ServiceImpl<GoodLinkMapper, GoodLink> implements GoodLinkService {

    @Autowired
    private BookService bookService;

    @Autowired
    private CourseService courseService;

    //根据用户id获得好物推荐列表
    @Override
    public Result<List<GoodVo>> getGoodBookList(String userId) {
        Result<List<GoodVo>> result = new Result<>();
        QueryWrapper<GoodLink> goodLinkQueryWrapper = new QueryWrapper<>();
        goodLinkQueryWrapper.eq("user_id", userId).eq("type", "book");
        List<GoodLink> goodLinkList = baseMapper.selectList(goodLinkQueryWrapper);
        if (goodLinkList == null) {
            result.setSuccess(false);
            result.setMessage("列表为空");
            return result;
        }
        List<GoodVo> goodVoList = new ArrayList<>();
        for (GoodLink goodLink : goodLinkList) {
            GoodVo goodVo = new GoodVo();
            if (goodLink.getGoodId()!=null){
                BeanUtils.copyProperties(goodLink, goodVo);
                Book book = bookService.getById(goodLink.getGoodId());
                BeanUtils.copyProperties(book,goodVo);
                goodVoList.add(goodVo);
            }
        }
        result.setResult(goodVoList);
        result.setSuccess(true);
        result.setMessage("查询成功");
        return result;
    }

    //根据用户id获得好物推荐列表-课程
    @Override
    public Result<List<GoodVo>> getGoodCourseList(String userId) {
        Result<List<GoodVo>> result = new Result<>();
        QueryWrapper<GoodLink> goodLinkQueryWrapper = new QueryWrapper<>();
        goodLinkQueryWrapper.eq("user_id", userId).eq("type", "course");
        List<GoodLink> goodLinkList = baseMapper.selectList(goodLinkQueryWrapper);
        if (goodLinkList == null) {
            result.setSuccess(false);
            result.setMessage("列表为空");
            return result;
        }
        List<GoodVo> goodVoList = new ArrayList<>();
        for (GoodLink goodLink : goodLinkList) {
            GoodVo goodVo = new GoodVo();
            if (goodLink.getGoodId()!=null){
                BeanUtils.copyProperties(goodLink, goodVo);
                Course course = courseService.getById(goodLink.getGoodId());
                BeanUtils.copyProperties(course,goodVo);
                goodVoList.add(goodVo);
            }
        }
        result.setResult(goodVoList);
        result.setSuccess(true);
        result.setMessage("查询成功");
        return result;
    }

}